﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using UpLoad.ServiceReference;
using System.IO;
using System.Windows.Browser;

namespace UpLoad
{
    public partial class MainPage : UserControl
    {
        private const int BUFFER_SIZE = 2097152;  //2MB  
        //private  int BUFFER_SIZE = 4*4096;
        private UserFile _file;
        private byte[] _buffer;
        private UpLoadServiceClient _client;
        bool isAbort = false;
        public MainPage()
        {
            InitializeComponent();



            _buffer = new byte[BUFFER_SIZE];
            _client = new UpLoadServiceClient();
            //事件处理   
            _client.ActionUploadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_ActionUploadCompleted);
            this.button1.IsEnabled = false;
            this.button3.IsEnabled = false;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            isAbort = false;
            int bytesRead = _file.FileStream.Read(_buffer, 0, _buffer.Length);
            if (bytesRead > 0)
            {
                //_file.BytesRead = bytesRead;

                // _client.ActionUploadAsync(_file.Name, _buffer, false);


                _file.BytesRead = bytesRead;
                byte[] toUp = new byte[bytesRead];
                Array.Copy(_buffer, toUp, toUp.Length);
                _client.ActionUploadAsync(_file.Name, toUp);
            }
            else
            {

                _file.FileStream.Close();
            }


            this.button1.IsEnabled = false;
            this.button3.IsEnabled = true;
            this.button2.IsEnabled = false;

        }



        //======================

        void _client_ActionUploadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null && !isAbort)
            {

                _file.BytesSent += _file.BytesRead;
                prgUpload.Value = Math.Ceiling((double)_file.BytesSent / (double)_buffer.Length);  //更新进度条   
                if (_file.BytesSent < _file.Length)
                {
                    int bytesRead = _file.FileStream.Read(_buffer, 0, _buffer.Length);
                    if (bytesRead > 0)
                    {
                        // _file.BytesRead = bytesRead;
                        // _client.ActionUploadAsync(_file.Name, _buffer, true);
                        _file.BytesRead = bytesRead;
                        byte[] toUp = new byte[bytesRead];
                        Array.Copy(_buffer, toUp, toUp.Length);
                        _client.ActionUploadAsync(_file.Name, toUp);
                        string sent = (_file.BytesSent / 1024).ToString();
                        string total = (_file.Length / 1024).ToString();
                        this.label1.Content = sent + " / " + total + " KB";
                    }
                    else
                    {

                        _file.FileStream.Close();
                    }
                }
                else
                {
                    this.label1.Content = "上传成功";
                    this.button3.IsEnabled = false;
                    this.button2.IsEnabled = true;
                    _file.FileStream.Close();
                }
            }
        }
        //=====================
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //选择本地文件对话框   
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "(*.*)|*.*";
            //单选   
            openFileDialog.Multiselect = false;

            if (openFileDialog.ShowDialog() == true)
            {

                _file = new UserFile();
                FileInfo fileInfo = openFileDialog.File;
                _file.Name = fileInfo.Name;
                _file.Length = fileInfo.Length;

                _file.BytesRead = 0;
                _file.BytesSent = 0;
                _file.FileStream = fileInfo.OpenRead();
                prgUpload.Minimum = 0;
                prgUpload.Maximum = Math.Ceiling((double)_file.Length / (double)_buffer.Length);
                this.textBlock1.Text = _file.Name.ToString();
                this.button1.IsEnabled = true;

                HtmlElement element = HtmlPage.Document.GetElementById("fileName");
                element.SetAttribute("value", this.textBlock1.Text);

            }
        }

        //=====================

        private void button3_Click(object sender, RoutedEventArgs e)
        {

            isAbort = true;
            this.button1.IsEnabled = true;
            this.button3.IsEnabled = false;

        }
    }
}
